package com.gudaomai.ekproject.test.assist;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.ProtocolException;
import java.net.URL;

import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.client.HttpResponse;

/**
 * 
 * @author xiaosong 用于辅助进行junit测试测试
 */

public class URLMethodUtils {
	// 用于用户输入测试url地址
	private String url;
	// 用于用户指定要测试的方法
	private String method;
	// 用于获取测试信息
	private StringBuffer sb;
	// 由输入的url地址生成url对象
	private URL urlPath;
	// httpUrl连接，用于发送信息和接收响应信息
	private HttpURLConnection connection;

	// 初始化URL和HttpURLConnection对象
	public void init(String url, String method, String params) throws Exception {
		urlPath = new URL(url);
		connection = (HttpURLConnection) urlPath.openConnection();
	}

	// 构造器，传入url,method,params
	public URLMethodUtils(String url, String method, String params) throws Exception {

		// 初始化连接
		init(url, method, params);

		// 对方法经行判断，给出相应的处理
		switch (method) {
		case AssistConstant.HTTP_METHOD_GET:
			sendGet(params);
			break;
		case AssistConstant.HTTP_METHOD_POST:
			senPost(url, params);
			break;
		case AssistConstant.HTTP_METHOD_PUT:
			sendPut(url, params);
			break;
		case AssistConstant.HTTP_METHOD_DELETE:
			sendDelete(url, params);
			break;
		default:
			// 其他四个请求不常见，暂时未定义
			break;
		}

	}

	// 处理get方法
	private void sendGet(String params) throws Exception {
		url = urlPath.toString() + params;
		URL urlT = new URL(url);
		connection = (HttpURLConnection) urlT.openConnection();
		connection.connect();
		BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8")); // 获取输入流
		String line = null;
		sb = new StringBuffer("");
		while ((line = br.readLine()) != null) {
			sb.append(line + "\n");
		}
		br.close();
		connection.disconnect();
	}

	// 处理post方法
	private void senPost(String url, String params) throws Exception {

		connection.setDoOutput(true);
		connection.setDoInput(true);
		connection.setRequestMethod(AssistConstant.HTTP_METHOD_POST);
		connection.setUseCaches(false);
		connection.setInstanceFollowRedirects(true);
		connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
		connection.connect();
		// POST请求
		DataOutputStream out = new DataOutputStream(connection.getOutputStream());
		out.write(params.getBytes("UTF-8"));
		out.flush();
		out.close();
		// 读取响应
		BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
		String lines;
		sb = new StringBuffer("");
		while ((lines = reader.readLine()) != null) {
			lines = new String(lines.getBytes(), "utf-8");
			sb.append(lines);
		}
		reader.close();
		// 断开连接
		connection.disconnect();
	}

	// 处理put请求
	private void sendPut(String url, String params) throws Exception {
		connection.setDoOutput(true);
		connection.setDoInput(true);
		connection.setRequestMethod(AssistConstant.HTTP_METHOD_PUT);
		connection.setUseCaches(false);
		connection.setInstanceFollowRedirects(true);
		connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
		connection.connect();

		// PUT请求
		DataOutputStream out = new DataOutputStream(connection.getOutputStream());
		out.write(params.getBytes("UTF-8"));
		out.flush();
		out.close();

		// 读取响应
		BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
		String lines;
		sb = new StringBuffer("");
		while ((lines = reader.readLine()) != null) {
			lines = new String(lines.getBytes(), "utf-8");
			sb.append(lines);
		}
		reader.close();
		// 断开连接
		connection.disconnect();
	}

	// 处理delete请求
	private void sendDelete(String url, String params) throws Exception {

		this.url = urlPath.toString() + "?" + params;
		URL urlT = new URL(this.url);
		connection = (HttpURLConnection) urlT.openConnection();
		connection.setDoOutput(false);
		connection.setDoInput(true);
		connection.setRequestMethod(AssistConstant.HTTP_METHOD_DELETE);
		connection.setUseCaches(false);
		connection.setInstanceFollowRedirects(true);
		connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
		connection.connect();

		System.out.println(connection.getResponseMessage());

		// 读取响应
		BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
		String lines;
		sb = new StringBuffer("");
		while ((lines = reader.readLine()) != null) {
			lines = new String(lines.getBytes(), "utf-8");
			sb.append(lines);
		}
		reader.close();
		// 断开连接
		connection.disconnect();
	}

	//////////////////////////////////////////////////////////////////////////////////
	//set，get方法
	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public StringBuffer getSb() {
		return sb;
	}

	public void setSb(StringBuffer sb) {
		this.sb = sb;
	}

	public String getMethod() {
		return method;
	}

	public void setMethod(String method) {
		this.method = method;
	}

	//获取返回状态信息
	public String getResponseInfo() {
		return getSb().toString();
	}

}
